home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / xinetd.2 / xinetd / xinetd.2.1.7-linux.4 / libs / src / sio / impl.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-10  |  5.6 KB  |  220 lines

  1. /*
  2.  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. /*
  8.  * $Id: impl.h,v 1.6 1995/09/10 18:42:47 chuck Exp $
  9.  */
  10.  
  11. #ifndef SIO_BUFFER_SIZE
  12.  
  13. #include "sioconf.h"
  14.  
  15. #ifdef HAS_MMAP
  16. #include <sys/types.h>
  17.  
  18.  
  19. /*
  20.  * A struct map_unit describes a memory mapped area of a file.
  21.  *
  22.  * addr is the address where the file is mapped. If addr is NULL
  23.  * the other fields are meaningless.
  24.  * valid_bytes indicates how many bytes are _valid_ in that unit
  25.  * mapped_bytes of a unit is how many bytes are mapped in that
  26.  * unit ( valid <= mapped ).
  27.  * Normally mapped_bytes will be equal to valid_bytes until
  28.  * we reach the end of the file. Then if the file size is not a multiple
  29.  * of the unit size, only the rest of the file will be mapped at the
  30.  * unit, leaving part of what was mapped at the unit before still
  31.  * visible (this happens because I chose not to unmap a unit before
  32.  * remapping it). In that case valid_bytes shows the size of the "new"
  33.  * mapping and mapped_bytes shows how many bytes are really mapped.
  34.  * mapped_bytes is used in Sdone() to unmap the units.
  35.  */
  36. struct map_unit
  37. {
  38.     caddr_t addr ;
  39.     size_t valid_bytes ;
  40.     size_t mapped_bytes ;
  41. } ;
  42.  
  43.  
  44. /*
  45.  * Meaning of fields used when memory mapping:
  46.  *
  47.  *    file_offset:      it is the offset in the file where the next
  48.  *                      mapping should be done
  49.  *
  50.  *    file_size:        size of the file (obtained from stat(2))
  51.  */
  52. struct mmap_descriptor
  53. {
  54.    off_t file_offset ;
  55.    off_t file_size ;
  56.     struct map_unit first_unit ;
  57.     struct map_unit second_unit ;
  58. } ;
  59.  
  60. typedef struct mmap_descriptor mapd_s ;
  61.  
  62. #endif /* HAS_MMAP */
  63.  
  64. typedef enum { FAILURE, SUCCESS } status_e ;
  65.  
  66. /*
  67.  * Descriptor management: convert a descriptor pointer to an input or
  68.  * output descriptor pointer
  69.  */
  70. #define IDP( dp )                        (&(dp)->descriptor.input_descriptor)
  71. #define ODP( dp )                        (&(dp)->descriptor.output_descriptor)
  72.  
  73. #define DESCRIPTOR_INITIALIZED( dp )    ((dp)->initialized)
  74.  
  75. /*
  76.  * Internal constants
  77.  */
  78. #define SIO_BUFFER_SIZE           8192
  79. #define SIO_NO_TIED_FD                (-1)
  80.  
  81. typedef enum { NO = 0, YES = 1 } boolean_e ;
  82.  
  83. #ifndef FALSE
  84. #define FALSE            0
  85. #define TRUE            1
  86. #endif
  87.  
  88. #ifndef NULL
  89. #define NULL            0
  90. #endif
  91.  
  92. #ifdef MIN
  93. #undef MIN
  94. #endif
  95. #define MIN( a, b )                    ( (a) < (b) ? (a) : (b) )
  96.  
  97. #define NUL                                '\0'
  98.  
  99. #define PRIVATE                        static
  100.  
  101. #ifdef DEBUG
  102.  
  103. static char *itoa( num )
  104.     unsigned num ;
  105. {
  106. #define NUMBUF_SIZE        15
  107.     static char numbuf[ NUMBUF_SIZE ] ;
  108.     register char *p = &numbuf[ NUMBUF_SIZE ] ;
  109.  
  110.     *--p = '\0' ;
  111.     do
  112.     {
  113.         *--p = num % 10 + '0' ;
  114.         num /= 10 ;
  115.     }
  116.     while ( num ) ;
  117.     return( p ) ;
  118. }
  119.  
  120. #    define ASSERT( expr )                                                        \
  121.         if ( ! (expr) )                                                            \
  122.         {                                                                                \
  123.             char *s1 = "SIO assertion << expr >> failed: File: " ;    \
  124.             char *s2 = __FILE__ ;                                                \
  125.             char *s3 = ", line: " ;                                                \
  126.             char *s4 = itoa( __LINE__ ) ;                                        \
  127.             char *s5 = "\n" ;                                                        \
  128.             (void) write( 2, s1, strlen( s1 ) ) ;                            \
  129.             (void) write( 2, s2, strlen( s2 ) ) ;                            \
  130.             (void) write( 2, s3, strlen( s3 ) ) ;                            \
  131.             (void) write( 2, s4, strlen( s4 ) ) ;                            \
  132.             (void) write( 2, s5, strlen( s5 ) ) ;                            \
  133.             exit ( 1 ) ;                                                            \
  134.         }
  135. #else
  136. #    define ASSERT( expr )
  137. #endif
  138.  
  139.  
  140. #include <errno.h>
  141. extern int errno ;
  142.  
  143. /*
  144.  * IO_SETUP initializes a descriptor if it is not already initialized.
  145.  * It checks if the stream is of the right type (input or output).
  146.  * CONTROL_SETUP checks if the descriptor is initialized and if the
  147.  * stream is of the right type (input or output). 
  148.  *
  149.  *     fd: file descriptor
  150.  *     dp: descriptor pointer
  151.  *     op: operation
  152.  *     ev: error value (if __sio_init fails; __sio_init should set errno) 
  153.  *
  154.  * IO_SETUP will call __sio_init if the descriptor is not initialized.
  155.  * Possible errors:
  156.  *        1. Using CONTROL_SETUP on an uninitialized descriptor.
  157.  *        2. The operation is not appropriate for the descriptor (e.g.
  158.  *            a read operation on an descriptor used for writing). 
  159.  * Both errors set errno to EBADF.
  160.  */
  161. #define CONTROL_SETUP( dp, type, ev )                                                        \
  162.             {                                                                                            \
  163.                 if ( ! DESCRIPTOR_INITIALIZED( dp ) || dp->stream_type != type )    \
  164.                 {                                                                                        \
  165.                     errno = EBADF ;                                                                \
  166.                     return( ev ) ;                                                                    \
  167.                 }                                                                                        \
  168.             }
  169.  
  170.  
  171. #define IO_SETUP( fd, dp, type, ev )                                                        \
  172.             {                                                                                            \
  173.                 if ( DESCRIPTOR_INITIALIZED( dp ) )                                         \
  174.                 {                                                                                        \
  175.                     if ( dp->stream_type != type )                                            \
  176.                     {                                                                                    \
  177.                         errno = EBADF ;                                                            \
  178.                         return( ev ) ;                                                                \
  179.                     }                                                                                    \
  180.                 }                                                                                        \
  181.                 else if ( __sio_init( dp, fd, type ) == SIO_ERR )                        \
  182.                     return( ev ) ;                                                                    \
  183.             }
  184.  
  185.  
  186. /*
  187.  * Internal functions that are visible
  188.  */
  189. int __sio_readf(), __sio_writef(), __sio_pwritef() ;
  190. int __sio_extend_buffer(), __sio_init(), __sio_converter(), __sio_more() ;
  191. status_e __sio_switch() ;
  192.  
  193.  
  194. #ifdef HAS_MEMOPS
  195. #include <memory.h>
  196. #define sio_memcopy( from, to, nbytes )       (void) memcpy( to, from, nbytes )
  197. #define sio_memscan( from, nbytes, ch )      memchr( from, ch, nbytes )
  198. #endif
  199.  
  200. #ifdef HAS_BCOPY
  201. #define sio_memcopy( from, to, nbytes )      (void) bcopy( from, to, nbytes )
  202. #endif
  203.  
  204. #ifndef sio_memcopy
  205. #define sio_memcopy        __sio_memcopy
  206. #define NEED_MEMCOPY
  207. void __sio_memcopy() ;
  208. #endif
  209.  
  210. #ifndef sio_memscan
  211. #if defined(linux) || defined(__FreeBSD__) || defined(__bsdi__) || defined(__NetBSD__)
  212. PRIVATE char *sio_memscan( char *from , int how_many , register char ch ) ;
  213. #else
  214. char *sio_memscan() ;
  215. #endif
  216. #endif
  217.  
  218. #endif /* SIO_BUFFER_SIZE */
  219.  
  220.